package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import module.ShopCartModule;

import bean.ResultFlag;
import bean.ShopCart;

import com.google.gson.Gson;

public class ShopCartController extends HttpServlet {

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/json");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		Gson gson=new Gson();
		ResultFlag flag=new ResultFlag();
		ShopCart shopcart=new ShopCart();
		ShopCartModule shopcartmodule=new ShopCartModule();
		String action=request.getParameter("action");
		//action select function
		if(action.equals("getAllShopCartMsg")){
			int userid=Integer.parseInt(request.getParameter("userid"));
			try {
				ArrayList list=shopcartmodule.getShopCartMsg(userid);
				String shopcartjson=gson.toJson(list);
				out.print(shopcartjson);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		else if(action.equals("delThisShop")){
			int carid=Integer.parseInt(request.getParameter("carid"));
			try {
				int delFlag=shopcartmodule.shopCartDel(carid);
				flag.setFlag(delFlag);
				String flagjson=gson.toJson(flag);
				out.print(flagjson);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		else if(action.equals("add")){
			int carid=Integer.parseInt(request.getParameter("carid"));
			int num=Integer.parseInt(request.getParameter("num"));
			try {
				int addFlag=shopcartmodule.shopCartChgNum(carid, num);
				flag.setFlag(addFlag);
				String flagjson=gson.toJson(flag);
				out.print(flagjson);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		else if(action.equals("sub")){
			int carid=Integer.parseInt(request.getParameter("carid"));
			int num=Integer.parseInt(request.getParameter("num"));
			try {
				int subFlag=shopcartmodule.shopCartChgNum(carid, num);
				flag.setFlag(subFlag);
				String flagjson=gson.toJson(flag);
				out.print(flagjson);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		else if(action.equals("pay")){
			int userid=Integer.parseInt(request.getParameter("userid"));
			float price=Float.parseFloat(request.getParameter("price"));
			try {
				int payFlag=shopcartmodule.shopCartPay(userid, price);
				flag.setFlag(payFlag);
				String flagjson=gson.toJson(flag);
				out.print(flagjson);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		out.flush();
		out.close();
	}

}
